package com.stock.demo.function.trade.view;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.stock.demo.R;
import com.stock.demo.adapter.AnimatorAdapter;
import com.stock.demo.adapter.TradeViewAdapter;
import com.stock.demo.framework.ui.fragment.BaseNetFragment;
import com.stock.demo.function.share.view.StockPositionActivity;
import com.stock.demo.model.TradeBean;
import com.stock.demo.util.CacheUtils;
import com.stock.demo.util.CommonUtils;
import com.stock.demo.util.GlideCircleTransform;

import java.util.ArrayList;

/**
 * Created by XuZhaoyu on 2017/6/1.
 */

public class TradeFragment extends BaseNetFragment {

    private static final String CACHE_KEY_HEAD = "cacheKeyHead";
    private View mView;
    private ImageView mIvEmuToggle;
    private LinearLayout mLlEmuUserInfo;
    private boolean mIsEmuUserInfoShow = true;
    private LinearLayout mLlEmuContent;
    private int mEmuContentHeight;
    private TextView mTvEmuTitle;
    private TextView mTvRealTitle;
    private LinearLayout mLlContent;
    private boolean mCurrentIsEmu = true;//记录当前是模拟页面还是实盘页面
    private int mContentWidth;
    private LinearLayout mLlEmuPage;
    private LinearLayout mLlRealPage;
    private ImageView mIvEmuUserHead;

    public static final int REQUEST_CODE_SELECT = 100;
    private final int mEmuUserInfoHeight = 100;//模拟交易用户信息界面的高度
    private RecyclerView mRvTradeList;
    private TradeViewAdapter mTradeViewAdapter;

    @Override
    public View onCreateSuccessView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_trade, container, false);
        return mView;
    }

    @Override
    public void onReload(View v) {

    }

    @Override
    public void initView() {
        mTvEmuTitle = (TextView) mView.findViewById(R.id.tv_trade_emu_title);
        mTvRealTitle = (TextView) mView.findViewById(R.id.tv_trade_real_title);
        mLlContent = (LinearLayout) mView.findViewById(R.id.ll_trade_content);

        //模拟界面
        mLlEmuPage = (LinearLayout) mView.findViewById(R.id.ll_trade_emu_page);
        mIvEmuUserHead = (ImageView) mView.findViewById(R.id.iv_trade_emu_user_head);
        mIvEmuToggle = (ImageView) mView.findViewById(R.id.iv_trade_emu_toggle);
        mLlEmuUserInfo = (LinearLayout) mView.findViewById(R.id.ll_trade_emu_user_info);
        mLlEmuContent = (LinearLayout) mView.findViewById(R.id.ll_trade_emu_content);
        mRvTradeList = (RecyclerView) mView.findViewById(R.id.rv_trade_list);
        mView.findViewById(R.id.ll_trade_emu_buy).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StockPositionActivity.startAction(getContext(), 0, null);
            }
        });
        mView.findViewById(R.id.ll_trade_emu_sell).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StockPositionActivity.startAction(getContext(), 1, null);
            }
        });
        mView.findViewById(R.id.ll_trade_emu_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StockPositionActivity.startAction(getContext(), 2, null);
            }
        });
        mView.findViewById(R.id.ll_trade_emu_query).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                StockPositionActivity.startAction(getContext(), 3, null);
            }
        });

        //实盘交易界面
        mLlRealPage = (LinearLayout) mView.findViewById(R.id.ll_trade_real_page);

        mTvEmuTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!mCurrentIsEmu) {
                    mTvEmuTitle.setTextColor(getResources().getColor(R.color.colorPrimary));
                    mTvEmuTitle.setBackgroundColor(getResources().getColor(R.color.colorWhite));
                    mTvRealTitle.setTextColor(getResources().getColor(R.color.colorWhite));
                    mTvRealTitle.setBackgroundColor(getResources().getColor(R.color.transparent));
                    showEmuContent();//切换模拟交易界面
                }
            }
        });
        mTvRealTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCurrentIsEmu) {
                    mTvEmuTitle.setTextColor(getResources().getColor(R.color.colorWhite));
                    mTvEmuTitle.setBackgroundColor(getResources().getColor(R.color.transparent));
                    mTvRealTitle.setTextColor(getResources().getColor(R.color.colorPrimary));
                    mTvRealTitle.setBackgroundColor(getResources().getColor(R.color.colorWhite));
                    showRealContent();//切换实盘交易界面
                }
            }
        });
        mIvEmuToggle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {//展示或隐藏模拟交易界面的用户信息
                if (mIsEmuUserInfoShow) {
                    hideEmuUserInfo();
                } else {
                    showEmuUserInfo();
                }
            }
        });
        mIvEmuUserHead.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ImagePicker imagePicker = ImagePicker.getInstance();
                imagePicker.setMultiMode(false);
                imagePicker.setCrop(true);
                BottomSelectDialog bottomSelectDialog = new BottomSelectDialog(getContext(), "拍照", "相册", new BottomSelectDialog.OnItemClickListener() {
                    @Override
                    public void item1Click() {
                        Intent intent = new Intent(getContext(), ImageGridActivity.class);
                        intent.putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true);// 是否是直接打开相机
                        startActivityForResult(intent, REQUEST_CODE_SELECT);
                    }

                    @Override
                    public void item2Click() {
                        Intent intent = new Intent(getContext(), ImageGridActivity.class);
                        startActivityForResult(intent, REQUEST_CODE_SELECT);
                    }
                });
                bottomSelectDialog.show();
            }
        });
        String path = CacheUtils.get().getAsString(CACHE_KEY_HEAD);
        if (!TextUtils.isEmpty(path)) {
            Glide.with(this).load(path).transform(new GlideCircleTransform(getContext())).into(mIvEmuUserHead);
        }
    }

    /**
     * 调用相机或照片返回的结果
     *
     * @param requestCode
     * @param resultCode
     * @param data
     */
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case REQUEST_CODE_SELECT:
                if (null != data) {
                    ArrayList<ImageItem> images = (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                    String path = images.get(0).path;
                    CacheUtils.get().put(CACHE_KEY_HEAD, path);
                    Glide.with(this).load(path).transform(new GlideCircleTransform(getContext())).into(mIvEmuUserHead);
                }
                break;
        }
    }

    private void showRealContent() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(mLlContent, "TranslationX", 0, -mContentWidth);
        animator.setDuration(300);
        animator.addListener(new AnimatorAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                mCurrentIsEmu = false;
            }
        });
        animator.start();
    }

    private void showEmuContent() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(mLlContent, "TranslationX", -mContentWidth, 0);
        animator.setDuration(300);
        animator.addListener(new AnimatorAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                mCurrentIsEmu = true;
            }
        });
        animator.start();
    }

    private void showEmuUserInfo() {
        ObjectAnimator toggleAnim = ObjectAnimator.ofFloat(mIvEmuToggle, "Rotation", 180f, 360f);
        ObjectAnimator userInfoAnim = ObjectAnimator.ofFloat(mLlEmuUserInfo, "TranslationY", -CommonUtils.dp2px(mEmuUserInfoHeight), 0f);
        ObjectAnimator contentAnim = ObjectAnimator.ofFloat(mLlEmuContent, "TranslationY", -CommonUtils.dp2px(mEmuUserInfoHeight), 0f);
        AnimatorSet animationSet = new AnimatorSet();
        animationSet.playTogether(toggleAnim, userInfoAnim, contentAnim);
        animationSet.setDuration(300);
        contentAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ViewGroup.LayoutParams layoutParams = mLlEmuContent.getLayoutParams();
                layoutParams.height = (int) (mEmuContentHeight - Double.valueOf(animation.getAnimatedValue().toString()));
                mLlEmuContent.setLayoutParams(layoutParams);
            }
        });
        animationSet.addListener(new AnimatorAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                mIvEmuToggle.setClickable(false);
                mIsEmuUserInfoShow = !mIsEmuUserInfoShow;
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mIvEmuToggle.setClickable(true);
            }
        });
        animationSet.start();
    }

    private void hideEmuUserInfo() {
        ObjectAnimator toggleAnim = ObjectAnimator.ofFloat(mIvEmuToggle, "Rotation", 0f, 180f);
        ObjectAnimator userInfoAnim = ObjectAnimator.ofFloat(mLlEmuUserInfo, "TranslationY", 0f, -CommonUtils.dp2px(mEmuUserInfoHeight));
        ObjectAnimator contentAnim = ObjectAnimator.ofFloat(mLlEmuContent, "TranslationY", 0f, -CommonUtils.dp2px(mEmuUserInfoHeight));
        AnimatorSet animationSet = new AnimatorSet();
        animationSet.playTogether(toggleAnim, userInfoAnim, contentAnim);
        animationSet.setDuration(300);
        contentAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                ViewGroup.LayoutParams layoutParams = mLlEmuContent.getLayoutParams();
                layoutParams.height = (int) (mEmuContentHeight - Double.valueOf(animation.getAnimatedValue().toString()));
                mLlEmuContent.setLayoutParams(layoutParams);
            }
        });
        animationSet.addListener(new AnimatorAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                mIvEmuToggle.setClickable(false);
                mIsEmuUserInfoShow = !mIsEmuUserInfoShow;
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                mIvEmuToggle.setClickable(true);
            }
        });
        animationSet.start();
    }

    @Override
    public void initData() {
        onNetNext(new Object()).setNetComplete();
        mLlContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
                mLlEmuContent.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                mContentWidth = CommonUtils.getScreenWidth(getContext());

                //将主体宽度设置为屏幕宽度的2倍
                ViewGroup.LayoutParams contentLayoutParams = mLlContent.getLayoutParams();
                contentLayoutParams.width = mContentWidth * 2;
                mLlContent.setLayoutParams(contentLayoutParams);

                //将每个页面设置为屏幕宽度
                ViewGroup.LayoutParams emuPageLayoutParams = mLlEmuPage.getLayoutParams();
                emuPageLayoutParams.width = mContentWidth;
                mLlEmuPage.setLayoutParams(emuPageLayoutParams);
                ViewGroup.LayoutParams realPageLayoutParams = mLlRealPage.getLayoutParams();
                realPageLayoutParams.width = mContentWidth;
                mLlRealPage.setLayoutParams(realPageLayoutParams);

                //获取模拟界面正文高度
                mEmuContentHeight = mLlEmuContent.getHeight();
            }
        });
        mRvTradeList.setLayoutManager(new LinearLayoutManager(getContext()));
        ArrayList<TradeBean> tradeBeen = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            tradeBeen.add(new TradeBean("东方财富", "22660", "-1846.13", "-7.53%", "2000", "2000", "12.25", "11.33"));
        }
        mTradeViewAdapter = new TradeViewAdapter(getContext(), tradeBeen);
        mRvTradeList.setAdapter(mTradeViewAdapter);
    }

}
